package net.pimpas.web.tests.bb;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import net.pimpas.web.jsf.managedbean.ManagedBeanAnnotation;

@ManagedBeanAnnotation(beanName="ajaxBean", beanScope="session")
public class AjaxBB {

	private String login;
	private String password;
	private Integer color;
	private List<SelectItem> colors;
	private volatile Integer next;
	private List<String> selectedCars;
	private List<SelectItem> cars;
	private List<SelectItem> visibleCars;
	private List<SelectItem> sports;
	private String filter;
	private List<String> selectedSports;
	
	public AjaxBB() {
		this.setColors(new ArrayList<SelectItem>());
		for(int i=0;i<10;i++)
			this.getColors().add(new SelectItem(new Integer(i), "Color Number "+(i+1)));
		
		this.setNext(10);
		this.setCars(new ArrayList<SelectItem>());
		this.setSelectedCars(new ArrayList<String>());
		this.setVisibleCars(new ArrayList<SelectItem>());
		this.getCars().add(new SelectItem("F250","F250"));
		this.getCars().add(new SelectItem("porsche","porsche"));
		this.getCars().add(new SelectItem("peugeot 307 sw","peugeot 307 sw"));
		this.getCars().add(new SelectItem("peugeot 407","peugeot 407"));
		this.getCars().add(new SelectItem("omega","omega"));
		this.getCars().add(new SelectItem("Ram Truck","Ram Truck"));
		this.getCars().add(new SelectItem("Lamborghini Diablo","Lamborghini Diablo"));
		this.getCars().add(new SelectItem("Lamborghini LM 00x","Lamborghini LM 00x"));
		this.setSelectedCars(new ArrayList<String>());
		this.setSports(new ArrayList<SelectItem>());
		this.getSports().add(new SelectItem("Soccer", "Soccer"));
		this.getSports().add(new SelectItem("Tenis", "Tenis"));
		this.getSports().add(new SelectItem("Fishing", "Fishing"));
		this.getSports().add(new SelectItem("Another", "Another"));
		
		this.getVisibleCars().addAll(this.getCars());
	}
	
	public void doFilter(ActionEvent evt) {
		this.setVisibleCars(new ArrayList<SelectItem>());
		if(this.getFilter() == null || "".equals(this.getFilter())) {
			this.getVisibleCars().addAll(this.getCars());
			return;
		}
		this.setFilter(this.getFilter().toLowerCase());
		
		for(SelectItem item: this.getCars())
			if(((String)item.getValue()).toLowerCase().startsWith(this.getFilter()))
				this.getVisibleCars().add(item);
		
	}
	
	public void processPasswordAjax(ActionEvent evt) {
		this.setPassword(this.getLogin().toUpperCase());
	}
	
	public void processColorsAjax(ActionEvent evt) {
		
		this.getColors().add(new SelectItem(this.getNext(), "Runtime Ajax ["+this.getNext()+"]"));
		this.setNext(this.getNext()+1);
	}
	
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getColor() {
		return color;
	}

	public void setColor(Integer color) {
		this.color = color;
	}

	public List<SelectItem> getColors() {
		return colors;
	}

	public void setColors(List<SelectItem> colors) {
		this.colors = colors;
	}

	public Integer getNext() {
		return next;
	}

	public void setNext(Integer next) {
		this.next = next;
	}

	public List<String> getSelectedCars() {
		return selectedCars;
	}

	public void setSelectedCars(List<String> selectedCars) {
		this.selectedCars = selectedCars;
	}

	public List<SelectItem> getCars() {
		return cars;
	}

	public void setCars(List<SelectItem> cars) {
		this.cars = cars;
	}

	public String getFilter() {
		return filter;
	}

	public void setFilter(String filter) {
		this.filter = filter;
	}

	public List<SelectItem> getVisibleCars() {
		return visibleCars;
	}

	public void setVisibleCars(List<SelectItem> visibleCars) {
		this.visibleCars = visibleCars;
	}

	public List<SelectItem> getSports() {
		return sports;
	}

	public void setSports(List<SelectItem> sports) {
		this.sports = sports;
	}
}